home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / calc202a.lha / calc-2.02a / macedit.el < prev    next >
Lisp/Scheme  |  1993-06-01  |  23KB  |  717 lines

  1. ;; Keyboard macro editor for GNU Emacs.  Version 1.05.
  2. ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. ;; Written by Dave Gillespie, daveg@synaptics.com.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  9. ;; accepts responsibility to anyone for the consequences of using it
  10. ;; or for whether it serves any particular purpose or works at all,
  11. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  12. ;; License for full details.
  13.  
  14. ;; Everyone is granted permission to copy, modify and redistribute
  15. ;; GNU Emacs, but only under the conditions described in the
  16. ;; GNU Emacs General Public License.   A copy of this license is
  17. ;; supposed to have been given to you along with GNU Emacs so you
  18. ;; can know your rights and responsibilities.  It should be in a
  19. ;; file named COPYING.  Among other things, the copyright notice
  20. ;; and this notice must be preserved on all copies.
  21.  
  22. ;; Installation:
  23. ;;   (autoload 'edit-kbd-macro "macedit" "Edit a named keyboard macro" t)
  24. ;;   (autoload 'edit-last-kbd-macro "macedit" "Edit a keyboard macro" t)
  25. ;;   (autoload 'read-kbd-macro "macedit" "Parse region as keyboard macro" t)
  26.  
  27.  
  28.  
  29. ;; To use, type `M-x edit-last-kbd-macro' to edit the most recently
  30. ;; defined keyboard macro.  If you have used `M-x name-last-kbd-macro'
  31. ;; to give a keyboard macro a name, type `M-x edit-kbd-macro' to edit
  32. ;; the macro by name.  When you are done editing, type `C-c C-c' to
  33. ;; record your changes back into the original keyboard macro.
  34.  
  35.  
  36.  
  37.  
  38. ;;; The user-level commands for editing macros.
  39.  
  40. ;;;###autoload
  41. (defun edit-last-kbd-macro (&optional prefix buffer hook)
  42.   "Edit the most recently defined keyboard macro."
  43.   (interactive "P")
  44.   (MacEdit-edit-macro last-kbd-macro
  45.               (function (lambda (x arg) (setq last-kbd-macro x)))
  46.               prefix buffer hook)
  47. )
  48.  
  49. ;;;###autoload
  50. (defun edit-kbd-macro (cmd &optional prefix buffer hook in-hook out-hook)
  51.   "Edit a keyboard macro which has been assigned a name by name-last-kbd-macro.
  52. \(See also edit-last-kbd-macro.)"
  53.   (interactive "CCommand name: \nP")
  54.   (and cmd
  55.        (MacEdit-edit-macro (if in-hook
  56.                    (funcall in-hook cmd)
  57.                  (symbol-function cmd))
  58.                (or out-hook
  59.                    (list 'lambda '(x arg)
  60.                      (list 'fset
  61.                        (list 'quote cmd)
  62.                        'x)))
  63.                prefix buffer hook cmd))
  64. )
  65.  
  66. ;;;###autoload
  67. (defun read-kbd-macro (start &optional end)
  68.   "Read the region as a keyboard macro definition.
  69. The region is interpreted as spelled-out keystrokes, e.g., `M-x abc RET'.
  70. The resulting macro is installed as the \"current\" keyboard macro.
  71.  
  72. Symbols:  RET, SPC, TAB, DEL, LFD, NUL; C-key; M-key.  (Must be uppercase.)
  73.           REM marks the rest of a line as a comment.
  74.           Whitespace is ignored; other characters are copied into the macro."
  75.   (interactive "r")
  76.   (if (stringp start)
  77.       (setq last-kbd-macro (MacEdit-parse-keys start))
  78.     (setq last-kbd-macro (MacEdit-parse-keys (buffer-substring start end)))
  79.     (if (and (string-match "\\`\C-x(" last-kbd-macro)
  80.          (string-match "\C-x)\\'" last-kbd-macro))
  81.     (setq last-kbd-macro (substring last-kbd-macro 2 -2))))
  82. )
  83.  
  84.  
  85.  
  86.  
  87. ;;; Formatting a keyboard macro as human-readable text.
  88.  
  89. (defun MacEdit-print-macro (macro-str local-map)
  90.   (let ((save-map (current-local-map))
  91.     (print-escape-newlines t)
  92.     key-symbol key-str key-last prefix-arg this-prefix)
  93.     (unwind-protect
  94.     (progn
  95.       (use-local-map local-map)
  96.       (while (MacEdit-peek-char)
  97.         (MacEdit-read-key)
  98.         (setq this-prefix prefix-arg)
  99.         (or (memq key-symbol '(digit-argument
  100.                    negative-argument
  101.                    universal-argument))
  102.         (null prefix-arg)
  103.         (progn
  104.           (cond ((consp prefix-arg)
  105.              (insert (format "prefix-arg (%d)\n"
  106.                      (car prefix-arg))))
  107.             ((eq prefix-arg '-)
  108.              (insert "prefix-arg -\n"))
  109.             ((numberp prefix-arg)
  110.              (insert (format "prefix-arg %d\n" prefix-arg))))
  111.           (setq prefix-arg nil)))
  112.         (cond ((null key-symbol)
  113.            (insert "type \"")
  114.            (MacEdit-insert-string macro-str)
  115.            (insert "\"\n")
  116.            (setq macro-str ""))
  117.           ((stringp key-symbol)   ; key defined by another kbd macro
  118.            (insert "type \"")
  119.            (MacEdit-insert-string key-symbol)
  120.            (insert "\"\n"))
  121.           ((eq key-symbol 'digit-argument)
  122.            (MacEdit-prefix-arg key-last nil prefix-arg))
  123.           ((eq key-symbol 'negative-argument)
  124.            (MacEdit-prefix-arg ?- nil prefix-arg))
  125.           ((eq key-symbol 'universal-argument)
  126.            (let* ((c-u 4) (argstartchar key-last)
  127.               (char (MacEdit-read-char)))
  128.              (while (= char argstartchar)
  129.                (setq c-u (* 4 c-u)
  130.                  char (MacEdit-read-char)))
  131.              (MacEdit-prefix-arg char c-u nil)))
  132.           ((eq key-symbol 'self-insert-command)
  133.            (insert "insert ")
  134.            (if (and (>= key-last 32) (<= key-last 126))
  135.                (let ((str ""))
  136.              (while (or (and (eq key-symbol
  137.                          'self-insert-command)
  138.                      (< (length str) 60)
  139.                      (>= key-last 32)
  140.                      (<= key-last 126))
  141.                     (and (memq key-symbol
  142.                            '(backward-delete-char
  143.                          delete-backward-char
  144.                          backward-delete-char-untabify))
  145.                      (> (length str) 0)))
  146.                (if (eq key-symbol 'self-insert-command)
  147.                    (setq str (concat str
  148.                          (char-to-string key-last)))
  149.                  (setq str (substring str 0 -1)))
  150.                (MacEdit-read-key))
  151.              (insert "\"" str "\"\n")
  152.              (MacEdit-unread-chars key-str))
  153.              (insert "\"")
  154.              (MacEdit-insert-string (char-to-string key-last))
  155.              (insert "\"\n")))
  156.           ((and (eq key-symbol 'quoted-insert)
  157.             (MacEdit-peek-char))
  158.            (insert "quoted-insert\n")
  159.            (let ((ch (MacEdit-read-char))
  160.              ch2)
  161.              (if (and (>= ch ?0) (<= ch ?7))
  162.              (progn
  163.                (setq ch (- ch ?0)
  164.                  ch2 (MacEdit-read-char))
  165.                (if ch2
  166.                    (if (and (>= ch2 ?0) (<= ch2 ?7))
  167.                    (progn
  168.                      (setq ch (+ (* ch 8) (- ch2 ?0))
  169.                        ch2 (MacEdit-read-char))
  170.                      (if ch2
  171.                      (if (and (>= ch2 ?0) (<= ch2 ?7))
  172.                          (setq ch (+ (* ch 8) (- ch2 ?0)))
  173.                        (MacEdit-unread-chars ch2))))
  174.                  (MacEdit-unread-chars ch2)))))
  175.              (if (or (and (>= ch ?0) (<= ch ?7))
  176.                  (< ch 32) (> ch 126))
  177.              (insert (format "type \"\\%03o\"\n" ch))
  178.                (insert "type \"" (char-to-string ch) "\"\n"))))
  179.           ((memq key-symbol '(isearch-forward
  180.                       isearch-backward
  181.                       isearch-forward-regexp
  182.                       isearch-backward-regexp))
  183.            (insert (symbol-name key-symbol) "\n")
  184.            (MacEdit-isearch-argument))
  185.           ((eq key-symbol 'execute-extended-command)
  186.            (MacEdit-read-argument obarray 'commandp))
  187.           (t
  188.            (let ((cust (get key-symbol 'MacEdit-print)))
  189.              (if cust
  190.              (funcall cust)
  191.                (insert (symbol-name key-symbol))
  192.                (indent-to 30)
  193.                (insert " # ")
  194.                (MacEdit-insert-string key-str)
  195.                (insert "\n")
  196.                (let ((int (MacEdit-get-interactive key-symbol)))
  197.              (if (string-match "\\`\\*" int)
  198.                  (setq int (substring int 1)))
  199.              (while (> (length int) 0)
  200.                (cond ((= (aref int 0) ?a)
  201.                   (MacEdit-read-argument
  202.                    obarray nil))
  203.                  ((memq (aref int 0) '(?b ?B ?D ?f ?F ?n
  204.                               ?s ?S ?x ?X))
  205.                   (MacEdit-read-argument))
  206.                  ((and (= (aref int 0) ?c)
  207.                        (MacEdit-peek-char))
  208.                   (insert "type \"")
  209.                   (MacEdit-insert-string
  210.                    (char-to-string
  211.                     (MacEdit-read-char)))
  212.                   (insert "\"\n"))
  213.                  ((= (aref int 0) ?C)
  214.                   (MacEdit-read-argument
  215.                    obarray 'commandp))
  216.                  ((= (aref int 0) ?k)
  217.                   (MacEdit-read-key)
  218.                   (if key-symbol
  219.                       (progn
  220.                     (insert "type \"")
  221.                     (MacEdit-insert-string key-str)
  222.                     (insert "\"\n"))
  223.                     (MacEdit-unread-chars key-str)))
  224.                  ((= (aref int 0) ?N)
  225.                   (or this-prefix
  226.                       (MacEdit-read-argument)))
  227.                  ((= (aref int 0) ?v)
  228.                   (MacEdit-read-argument
  229.                    obarray 'user-variable-p)))
  230.                (let ((nl (string-match "\n" int)))
  231.                  (setq int (if nl
  232.                        (substring int (1+ nl))
  233.                      "")))))))))))
  234.       (use-local-map save-map)))
  235. )
  236.  
  237. (defun MacEdit-prefix-arg (char c-u value)
  238.   (let ((sign 1))
  239.     (if (and (numberp value) (< value 0))
  240.     (setq sign -1 value (- value)))
  241.     (if (eq value '-)
  242.     (setq sign -1 value nil))
  243.     (while (and char (= ?- char))
  244.       (setq sign (- sign) c-u nil)
  245.       (setq char (MacEdit-read-char)))
  246.     (while (and char (>= char ?0) (<= char ?9))
  247.       (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)) c-u nil)
  248.       (setq char (MacEdit-read-char)))
  249.     (setq prefix-arg
  250.       (cond (c-u (list c-u))
  251.         ((numberp value) (* value sign))
  252.         ((= sign -1) '-)))
  253.     (MacEdit-unread-chars char))
  254. )
  255.  
  256. (defun MacEdit-insert-string (str)
  257.   (let ((i 0) j ch)
  258.     (while (< i (length str))
  259.       (if (and (> (setq ch (aref str i)) 127)
  260.            (< ch 160))
  261.       (progn
  262.         (setq ch (- ch 128))
  263.         (insert "\\M-")))
  264.       (if (< ch 32)
  265.       (cond ((= ch 8)  (insert "\\b"))
  266.         ((= ch 9)  (insert "\\t"))
  267.         ((= ch 10) (insert "\\n"))
  268.         ((= ch 13) (insert "\\r"))
  269.         ((= ch 27) (insert "\\e"))
  270.         (t (insert "\\C-" (char-to-string (downcase (+ ch 64))))))
  271.     (if (< ch 127)
  272.         (if (or (= ch 34) (= ch 92))
  273.         (insert "\\" (char-to-string ch))
  274.           (setq j i)
  275.           (while (and (< (setq i (1+ i)) (length str))
  276.               (>= (setq ch (aref str i)) 32)
  277.               (/= ch 34) (/= ch 92)
  278.               (< ch 127)))
  279.           (insert (substring str j i))
  280.           (setq i (1- i)))
  281.       (if (memq ch '(127 255))
  282.           (insert (format "\\%03o" ch))
  283.         (insert "\\M-" (char-to-string (- ch 128))))))
  284.       (setq i (1+ i))))
  285. )
  286.  
  287. (defun MacEdit-lookup-key (map)
  288.   (let ((loc (and map (lookup-key map macro-str)))
  289.     (glob (lookup-key (current-global-map) macro-str))
  290.     (loc-str macro-str)
  291.     (glob-str macro-str))
  292.     (and (integerp loc)
  293.      (setq loc-str (substring macro-str 0 loc)
  294.            loc (lookup-key map loc-str)))
  295.     (and (consp loc)
  296.      (setq loc nil))
  297.     (or loc
  298.     (setq loc-str ""))
  299.     (and (integerp glob)
  300.      (setq glob-str (substring macro-str 0 glob)
  301.            glob (lookup-key (current-global-map) glob-str)))
  302.     (and (consp glob)
  303.      (setq glob nil))
  304.     (or glob
  305.     (setq glob-str ""))
  306.     (if (> (length glob-str) (length loc-str))
  307.     (setq key-symbol glob
  308.           key-str glob-str)
  309.       (setq key-symbol loc
  310.         key-str loc-str))
  311.     (setq key-last (and (> (length key-str) 0)
  312.             (logand (aref key-str (1- (length key-str))) 127)))
  313.     key-symbol)
  314. )
  315.  
  316. (defun MacEdit-read-argument (&optional obarray pred)   ;; currently ignored
  317.   (let ((str "")
  318.     (min-bsp 0)
  319.     (exec (eq key-symbol 'execute-extended-command))
  320.     str-base)
  321.     (while (progn
  322.          (MacEdit-lookup-key (current-global-map))
  323.          (or (and (eq key-symbol 'self-insert-command)
  324.               (< (length str) 60))
  325.          (memq key-symbol
  326.                 '(backward-delete-char
  327.                   delete-backward-char
  328.                   backward-delete-char-untabify))
  329.          (eq key-last 9)))
  330.       (setq macro-str (substring macro-str (length key-str)))
  331.       (or (and (eq key-last 9)
  332.            obarray
  333.            (let ((comp (try-completion str obarray pred)))
  334.          (and (stringp comp)
  335.               (> (length comp) (length str))
  336.               (setq str comp))))
  337.       (if (or (eq key-symbol 'self-insert-command)
  338.           (and (or (eq key-last 9)
  339.                (<= (length str) min-bsp))
  340.                (setq min-bsp (+ (length str) (length key-str)))))
  341.           (setq str (concat str key-str))
  342.         (setq str (substring str 0 -1)))))
  343.     (setq str-base str
  344.       str (concat str key-str)
  345.       macro-str (substring macro-str (length key-str)))
  346.     (if exec
  347.     (let ((comp (try-completion str-base obarray pred)))
  348.       (if (if (stringp comp)
  349.           (and (commandp (intern comp))
  350.                (setq str-base comp))
  351.         (commandp (intern str-base)))
  352.           (insert str-base "\n")
  353.         (insert "execute-extended-command\n")
  354.         (insert "type \"")
  355.         (MacEdit-insert-string str)
  356.         (insert "\"\n")))
  357.       (if (> (length str) 0)
  358.       (progn
  359.         (insert "type \"")
  360.         (MacEdit-insert-string str)
  361.         (insert "\"\n")))))
  362. )
  363.  
  364. (defun MacEdit-isearch-argument ()
  365.   (let ((str "")
  366.     (min-bsp 0)
  367.     ch)
  368.     (while (and (setq ch (MacEdit-read-char))
  369.         (or (<= ch 127) (not search-exit-option))
  370.         (not (eq ch search-exit-char))
  371.         (or (eq ch search-repeat-char)
  372.             (eq ch search-reverse-char)
  373.             (eq ch search-delete-char)
  374.             (eq ch search-yank-word-char)
  375.             (eq ch search-yank-line-char)
  376.             (eq ch search-quote-char)
  377.             (eq ch ?\r)
  378.             (eq ch ?\t)
  379.             (not search-exit-option)
  380.             (and (/= ch 127) (>= ch 32))))
  381.       (if (and (eq ch search-quote-char)
  382.            (MacEdit-peek-char))
  383.       (setq str (concat str (char-to-string ch)
  384.                 (char-to-string (MacEdit-read-char)))
  385.         min-bsp (length str))
  386.     (if (or (and (< ch 127) (>= ch 32))
  387.         (eq ch search-yank-word-char)
  388.         (eq ch search-yank-line-char)
  389.         (and (or (not (eq ch search-delete-char))
  390.              (<= (length str) min-bsp))
  391.              (setq min-bsp (1+ (length str)))))
  392.         (setq str (concat str (char-to-string ch)))
  393.       (setq str (substring str 0 -1)))))
  394.     (if (eq ch search-exit-char)
  395.     (if (= (length str) 0)  ;; non-incremental search
  396.         (progn
  397.           (setq str (concat str (char-to-string ch)))
  398.           (and (eq (MacEdit-peek-char) ?\C-w)
  399.            (progn
  400.              (setq str (concat str "\C-w"))
  401.              (MacEdit-read-char)))
  402.           (if (> (length str) 0)
  403.           (progn
  404.             (insert "type \"")
  405.             (MacEdit-insert-string str)
  406.             (insert "\"\n")))
  407.           (MacEdit-read-argument)
  408.           (setq str "")))
  409.       (MacEdit-unread-chars ch))
  410.     (if (> (length str) 0)
  411.     (progn
  412.       (insert "type \"")
  413.       (MacEdit-insert-string str)
  414.       (insert "\\e\"\n"))))
  415. )
  416.  
  417. ;;; Get the next keystroke-sequence from the input stream.
  418. ;;; Sets key-symbol, key-str, and key-last as a side effect.
  419. (defun MacEdit-read-key ()
  420.   (MacEdit-lookup-key (current-local-map))
  421.   (and key-symbol
  422.        (setq macro-str (substring macro-str (length key-str))))
  423. )
  424.  
  425. (defun MacEdit-peek-char ()
  426.   (and (> (length macro-str) 0)
  427.        (aref macro-str 0))
  428. )
  429.  
  430. (defun MacEdit-read-char ()
  431.   (and (> (length macro-str) 0)
  432.        (prog1
  433.        (aref macro-str 0)
  434.      (setq macro-str (substring macro-str 1))))
  435. )
  436.  
  437. (defun MacEdit-unread-chars (chars)
  438.   (and (integerp chars)
  439.        (setq chars (char-to-string chars)))
  440.   (and chars
  441.        (setq macro-str (concat chars macro-str)))
  442. )
  443.  
  444. (defun MacEdit-dump (mac)
  445.   (set-mark-command nil)
  446.   (insert "\n\n")
  447.   (MacEdit-print-macro mac (current-local-map))
  448. )
  449.  
  450.  
  451.  
  452. ;;; Parse a string of spelled-out keystrokes, as produced by key-description.
  453.  
  454. (defun MacEdit-parse-keys (str)
  455.   (let ((pos 0)
  456.     (mac "")
  457.     part)
  458.     (while (and (< pos (length str))
  459.         (string-match "[^ \t\n]+" str pos))
  460.       (setq pos (match-end 0)
  461.         part (substring str (match-beginning 0) (match-end 0))
  462.         mac (concat mac
  463.             (if (and (> (length part) 2)
  464.                  (= (aref part 1) ?-)
  465.                  (= (aref part 0) ?M))
  466.                 (progn
  467.                   (setq part (substring part 2))
  468.                   "\e")
  469.               (if (and (> (length part) 4)
  470.                    (= (aref part 0) ?C)
  471.                    (= (aref part 1) ?-)
  472.                    (= (aref part 2) ?M)
  473.                    (= (aref part 3) ?-))
  474.                   (progn
  475.                 (setq part (concat "C-" (substring part 4)))
  476.                 "\e")
  477.                 ""))
  478.             (or (cdr (assoc part '( ( "NUL" . "\0" )
  479.                         ( "RET" . "\r" )
  480.                         ( "LFD" . "\n" )
  481.                         ( "TAB" . "\t" )
  482.                         ( "ESC" . "\e" )
  483.                         ( "SPC" . " " )
  484.                         ( "DEL" . "\177" )
  485.                         ( "C-?" . "\177" )
  486.                         ( "C-2" . "\0" )
  487.                         ( "C-SPC" . "\0") )))
  488.                 (and (equal part "REM")
  489.                  (setq pos (or (string-match "\n" str pos)
  490.                            (length str)))
  491.                  "")
  492.                 (and (= (length part) 3)
  493.                  (= (aref part 0) ?C)
  494.                  (= (aref part 1) ?-)
  495.                  (char-to-string (logand (aref part 2) 31)))
  496.                 part))))
  497.     mac)
  498. )
  499.  
  500.  
  501.  
  502.  
  503. ;;; Parse a keyboard macro description in MacEdit-print-macro's format.
  504.  
  505. (defun MacEdit-read-macro (&optional map)
  506.   (or map (setq map (current-local-map)))
  507.   (let ((macro-str ""))
  508.     (while (not (progn
  509.           (skip-chars-forward " \t\n")
  510.           (eobp)))
  511.       (cond ((looking-at "#"))   ;; comment
  512.         ((looking-at "prefix-arg[ \t]*-[ \t]*\n")
  513.          (MacEdit-append-chars "\C-u-"))
  514.         ((looking-at "prefix-arg[ \t]*\\(-?[0-9]+\\)[ \t]*\n")
  515.          (MacEdit-append-chars (concat "\C-u" (MacEdit-match-string 1))))
  516.         ((looking-at "prefix-arg[ \t]*(\\([0-9]+\\))[ \t]*\n")
  517.          (let ((val (string-to-int (MacEdit-match-string 1))))
  518.            (while (> val 1)
  519.          (or (= (% val 4) 0)
  520.              (error "Bad prefix argument value"))
  521.          (MacEdit-append-chars "\C-u")
  522.          (setq val (/ val 4)))))
  523.         ((looking-at "prefix-arg")
  524.          (error "Bad prefix argument syntax"))
  525.         ((looking-at "insert ")
  526.          (forward-char 7)
  527.          (MacEdit-append-chars (read (current-buffer)))
  528.          (if (< (current-column) 7)
  529.          (forward-line -1)))
  530.         ((looking-at "type ")
  531.          (forward-char 5)
  532.          (MacEdit-append-chars (read (current-buffer)))
  533.          (if (< (current-column) 5)
  534.          (forward-line -1)))
  535.         ((looking-at "keys \\(.*\\)\n")
  536.          (goto-char (1- (match-end 0)))
  537.          (MacEdit-append-chars (MacEdit-parse-keys
  538.                     (buffer-substring (match-beginning 1)
  539.                               (match-end 1)))))
  540.         ((looking-at "\\([-a-zA-z0-9_]+\\)[ \t]*\\(.*\\)\n")
  541.          (let* ((func (intern (MacEdit-match-string 1)))
  542.             (arg (MacEdit-match-string 2))
  543.             (cust (get func 'MacEdit-read)))
  544.            (if cust
  545.            (funcall cust arg)
  546.          (or (commandp func)
  547.              (error "Not an Emacs command"))
  548.          (or (equal arg "")
  549.              (string-match "\\`#" arg)
  550.              (error "Unexpected argument to command"))
  551.          (let ((keys
  552.             (or (where-is-internal func map t)
  553.                 (where-is-internal func (current-global-map) t))))
  554.            (if keys
  555.                (MacEdit-append-chars keys)
  556.              (MacEdit-append-chars (concat "\ex"
  557.                            (symbol-name func)
  558.                            "\n")))))))
  559.         (t (error "Syntax error")))
  560.       (forward-line 1))
  561.     macro-str)
  562. )
  563.  
  564. (defun MacEdit-append-chars (chars)
  565.   (setq macro-str (concat macro-str chars))
  566. )
  567.  
  568. (defun MacEdit-match-string (n)
  569.   (if (match-beginning n)
  570.       (buffer-substring (match-beginning n) (match-end n))
  571.     "")
  572. )
  573.  
  574.  
  575.  
  576. (defun MacEdit-get-interactive (func)
  577.   (if (symbolp func)
  578.       (let ((cust (get func 'MacEdit-interactive)))
  579.     (if cust
  580.         cust
  581.       (MacEdit-get-interactive (symbol-function func))))
  582.     (or (and (eq (car-safe func) 'lambda)
  583.          (let ((int (if (consp (nth 2 func))
  584.                 (nth 2 func)
  585.               (nth 3 func))))
  586.            (and (eq (car-safe int) 'interactive)
  587.             (stringp (nth 1 int))
  588.             (nth 1 int))))
  589.     ""))
  590. )
  591.  
  592. (put 'search-forward           'MacEdit-interactive "s")
  593. (put 'search-backward          'MacEdit-interactive "s")
  594. (put 'word-search-forward      'MacEdit-interactive "s")
  595. (put 'word-search-backward     'MacEdit-interactive "s")
  596. (put 're-search-forward        'MacEdit-interactive "s")
  597. (put 're-search-backward       'MacEdit-interactive "s")
  598. (put 'switch-to-buffer         'MacEdit-interactive "B")
  599. (put 'kill-buffer              'MacEdit-interactive "B")
  600. (put 'rename-buffer            'MacEdit-interactive "B\nB")
  601. (put 'goto-char                'MacEdit-interactive "N")
  602. (put 'global-set-key           'MacEdit-interactive "k\nC")
  603. (put 'global-unset-key         'MacEdit-interactive "k")
  604. (put 'local-set-key            'MacEdit-interactive "k\nC")
  605. (put 'local-unset-key          'MacEdit-interactive "k")
  606.  
  607. ;;; Think about kbd-macro-query
  608.  
  609.  
  610.  
  611. ;;; Edit a keyboard macro in another buffer.
  612. ;;; (Prefix argument is currently ignored.)
  613.  
  614. (defun MacEdit-edit-macro (mac repl &optional prefix buffer hook arg)
  615.   (or (stringp mac)
  616.       (error "Not a keyboard macro"))
  617.   (let ((oldbuf (current-buffer))
  618.     (from-calc (and (get-buffer-window "*Calculator*")
  619.             (eq (lookup-key (current-global-map) "\e#")
  620.                 'calc-dispatch)))
  621.     (local (current-local-map))
  622.     (buf (get-buffer-create (or buffer "*Edit Macro*"))))
  623.     (set-buffer buf)
  624.     (kill-all-local-variables)
  625.     (use-local-map MacEdit-mode-map)
  626.     (setq buffer-read-only nil)
  627.     (setq major-mode 'MacEdit-mode)
  628.     (setq mode-name "Edit Macro")
  629.     (make-local-variable 'MacEdit-original-buffer)
  630.     (setq MacEdit-original-buffer oldbuf)
  631.     (make-local-variable 'MacEdit-replace-function)
  632.     (setq MacEdit-replace-function repl)
  633.     (make-local-variable 'MacEdit-replace-argument)
  634.     (setq MacEdit-replace-argument arg)
  635.     (make-local-variable 'MacEdit-finish-hook)
  636.     (setq MacEdit-finish-hook hook)
  637.     (erase-buffer)
  638.     (insert "# Keyboard Macro Editor.  Press "
  639.         (if from-calc "M-# M-#" "C-c C-c")
  640.         " to finish; press "
  641.         (if from-calc "M-# x" "C-x k RET")
  642.         " to cancel.\n")
  643.     (insert "# Original keys: " (key-description mac) "\n\n")
  644.     (message "Formatting keyboard macro...")
  645.     (MacEdit-print-macro mac local)
  646.     (switch-to-buffer buf)
  647.     (goto-char (point-min))
  648.     (forward-line 3)
  649.     (recenter '(4))
  650.     (set-buffer-modified-p nil)
  651.     (message "Formatting keyboard macro...done")
  652.     (run-hooks 'MacEdit-format-hook))
  653. )
  654.  
  655. (defun MacEdit-finish-edit ()
  656.   (interactive)
  657.   (or (and (boundp 'MacEdit-original-buffer)
  658.        (boundp 'MacEdit-replace-function)
  659.        (boundp 'MacEdit-replace-argument)
  660.        (boundp 'MacEdit-finish-hook)
  661.        (eq major-mode 'MacEdit-mode))
  662.       (error "This command is valid only in buffers created by edit-kbd-macro."))
  663.   (let ((buf (current-buffer))
  664.     (str (buffer-string))
  665.     (func MacEdit-replace-function)
  666.     (arg MacEdit-replace-argument)
  667.     (hook MacEdit-finish-hook))
  668.     (goto-char (point-min))
  669.     (and (buffer-modified-p)
  670.      func
  671.      (progn
  672.        (message "Compiling keyboard macro...")
  673.        (run-hooks 'MacEdit-compile-hook)
  674.        (let ((mac (MacEdit-read-macro
  675.                (and (buffer-name MacEdit-original-buffer)
  676.                 (save-excursion
  677.                   (set-buffer MacEdit-original-buffer)
  678.                   (current-local-map))))))
  679.          (and (buffer-name MacEdit-original-buffer)
  680.           (switch-to-buffer MacEdit-original-buffer))
  681.          (funcall func mac arg))
  682.        (message "Compiling keyboard macro...done")))
  683.     (kill-buffer buf)
  684.     (if hook
  685.     (funcall hook arg)))
  686. )
  687.  
  688. (defun MacEdit-cancel-edit ()
  689.   (interactive)
  690.   (if (eq major-mode 'MacEdit-mode)
  691.       (set-buffer-modified-p nil))
  692.   (MacEdit-finish-edit)
  693.   (message "(Cancelled)")
  694. )
  695.  
  696. (defun MacEdit-mode ()
  697.   "Keyboard Macro Editing mode.  Press C-c C-c to save and exit.
  698. To abort the edit, just kill this buffer with C-x k RET.
  699.  
  700. The keyboard macro is represented as a series of M-x style command names.
  701. Keystrokes which do not correspond to simple M-x commands are written as
  702. \"type\" commands.  When you press C-c C-c, MacEdit converts each command
  703. back into a suitable keystroke sequence; \"type\" commands are converted
  704. directly back into keystrokes."
  705.   (interactive)
  706.   (error "This mode can be enabled only by edit-kbd-macro or edit-last-kbd-macro.")
  707. )
  708. (put 'MacEdit-mode 'mode-class 'special)
  709.  
  710. (defvar MacEdit-mode-map nil)
  711. (if MacEdit-mode-map
  712.     ()
  713.   (setq MacEdit-mode-map (make-sparse-keymap))
  714.   (define-key MacEdit-mode-map "\C-c\C-c" 'MacEdit-finish-edit)
  715. )
  716.  
  717.